#include "../include/server.h"

int myls3(int sockFd, int ownerID, int curDirID){
    //查询当前目录有什么文件
    //成功返回1，失败返回0
    Train_t train;
    memset(&train, 0, sizeof(train));

    //使用索引数组修改结构体的内容
    FileInfo *files = (pFileInfo)calloc(50, sizeof(FileInfo));
    pFileInfo filesArr[50];
    for(int i = 0; i < 50; ++i){
        filesArr[i] = &files[i];
    }

    int fileCnt = query_dir(ownerID, curDirID, filesArr);
    printf("fileCnt = %d\n", fileCnt);

    //先把当前目录的文件数目发送过去
    train.dataLen = 4;
    memcpy(train.buf, &fileCnt, train.dataLen);
    send(sockFd, &train, 4+train.dataLen, 0);

    for(int i = 0; i < fileCnt; ++i){
        /* printf("filename = %s, filesize = %ld, filetype = %c\n", */
        /*        filesArr[i]->fileName, filesArr[i]->fileSize, filesArr[i]->type); */
        send(sockFd, (char*)filesArr[i], sizeof(files[i]), 0);
    }
    free(files);
    files = NULL;
    return 1;
}
